package com.lin.springcloud.controller;

import com.lin.springcloud.pojo.Dept;
import com.lin.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ author:lin tingfeng
 * @ data:2021/10/9-16:35
 */
@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private DiscoveryClient discoveryClient;//获取一些配置的信息，得到具体的微服务

    @PostMapping("/dept/add")
    public boolean addDept(Dept dept) {
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable Long id) {
        return deptService.queryDeptById(id);
    }

    @GetMapping("/dept/list")
    public List<Dept> getAll() {
        return deptService.queryDeptAll();
    }

    // 查看注册进来的微服务，获取一些消息
    @GetMapping("/dept/discovery")
    public Object discovery() {
        //获取微服务列表清单
        List<String> services = discoveryClient.getServices();
        System.out.println("discovery ==> services:" + services);

        //得到一个具体的微服务信息
        List<ServiceInstance> instances = discoveryClient.getInstances("springcloud-provider-dept-8002");
        for (ServiceInstance instance : instances
        ) {
            System.out.println(instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri() + "\t" + instance.getServiceId());
        }
        return this.discoveryClient;
    }
}

